﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Tetris.Interfaces;

namespace Tetris
{
    public class StandardInputManager :IInputManager
    {

        private KeyboardState _previousKeyboardState;
        private KeyboardState _currentKeyboardState;

        public StandardInputManager()
        {
            TouchPanel.EnabledGestures = GestureType.FreeDrag;
        }

        public void UpdateKeyboardState()
        {
            _previousKeyboardState = _currentKeyboardState;
            _currentKeyboardState = Keyboard.GetState();
        }

        public Vector2 GetInputVector( PlayerIndex playerIndex, float moveSpeed )
        {
            var result = new Vector2( 0, 0 );
            while( TouchPanel.IsGestureAvailable )
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                if( gesture.GestureType == GestureType.FreeDrag )
                {
                    result += gesture.Delta;
                }
            }

            result.X += GamePad.GetState( playerIndex ).ThumbSticks.Left.X * moveSpeed;
            result.Y -= GamePad.GetState( playerIndex ).ThumbSticks.Left.Y * moveSpeed;

            if( Keyboard.GetState().IsKeyDown( Keys.Left ) ||
                GamePad.GetState( playerIndex ).DPad.Left == ButtonState.Pressed )
            {
                result.X -= moveSpeed;
            }

            if( Keyboard.GetState().IsKeyDown( Keys.Right ) ||
                GamePad.GetState( playerIndex ).DPad.Right == ButtonState.Pressed )
            {
                result.X += moveSpeed;
            }

            if( Keyboard.GetState().IsKeyDown( Keys.Up ) ||
                GamePad.GetState( playerIndex ).DPad.Up == ButtonState.Pressed )
            {
                result.Y -= moveSpeed;
            }

            if( Keyboard.GetState().IsKeyDown( Keys.Down ) ||
                GamePad.GetState( playerIndex ).DPad.Down == ButtonState.Pressed )
            {
                result.Y += moveSpeed;
            }

            return result;
        }

        public bool IsBlockRotated()
        {
            return Keyboard.GetState().IsKeyDown(Keys.Space) && !_previousKeyboardState.IsKeyDown(Keys.Space);
        }

        public bool IsBlockMovedToLeft()
        {
            return Keyboard.GetState().IsKeyDown(Keys.Left) && !_previousKeyboardState.IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A) && !_previousKeyboardState.IsKeyDown(Keys.A);
        }

        public bool IsBlockMovedToRight()
        {
            return Keyboard.GetState().IsKeyDown(Keys.Right) && !_previousKeyboardState.IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D) && !_previousKeyboardState.IsKeyDown(Keys.D);
        }

        public bool IsBlockMovedDown()
        {
            return Keyboard.GetState().IsKeyDown(Keys.Down) || Keyboard.GetState().IsKeyDown(Keys.S);
        }

        public bool IsMenuUp( PlayerIndex? controllingPlayer )
        {
            return Keyboard.GetState().IsKeyDown( Keys.Up ) && !_previousKeyboardState.IsKeyDown(Keys.Up);
        }

        public bool IsMenuDown( PlayerIndex? controllingPlayer )
        {
            return Keyboard.GetState().IsKeyDown(Keys.Down) && !_previousKeyboardState.IsKeyDown(Keys.Down);
        }

        public bool IsMenuSelect( PlayerIndex? controllingPlayer )
        {
            return Keyboard.GetState().IsKeyDown( Keys.Enter );
        }

        public bool IsMenuCancel( PlayerIndex? controllingPlayer )
        {
            return Keyboard.GetState().IsKeyDown( Keys.Escape );
        }
    }
}
